package cz.gelion.shylock.accounting

import java.io.PrintWriter
import org.squeryl.Session
import org.squeryl.adapters.H2Adapter
import org.squeryl.dsl._
import org.squeryl.Schema
import org.squeryl.dsl.ast.EqualityExpression
import org.squeryl.dsl.ast.ConstantExpressionNode
import org.squeryl.customtypes.CustomTypesMode._
import org.squeryl.customtypes._

object Datastore extends Schema {
  
  
  val archive = table[Archive]("archive")
  val ledgerEvent = table[LedgerEvent]("event")
  val subject = table[Subject]("subject")
  val ledgerEventItem = table[LedgerEventItem]("item")
  
  
  val eventToSubject = oneToManyRelation(subject, ledgerEvent).via((s,e) => (s.id   === e.id))
  val archiveToEvent = manyToManyRelation(archive,ledgerEvent, "event_archive").via[LedgerEventArchive]((a,e,ae) => (ae.archiveId === e.id, a.id === ae.eventId))
  val eventToItem = oneToManyRelation(ledgerEvent,ledgerEventItem).via((e,i) => (e.id === i.id))
  
  
  
  def main(args: Array[String]) {
    
    
    Class.forName("org.h2.Driver");

    val ses = Session.create(
      java.sql.DriverManager.getConnection("jdbc:h2:file:data/resultsA", "sa", "password"),
      new H2Adapter
    )

    ses.bindToCurrentThread
    printDdl
  }
  
}

